package com.cbtx.module_sp;

import android.app.Activity;
import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;

import androidx.annotation.Nullable;

import com.cbtx.module.base.ui.BaseActivity;
import com.cbtx.module.common.bean.BannerBean;
import com.cbtx.module_sp.ui.activity.PlayActivity;
import com.cbtx.module_sp.ui.vm.MainVM;
import com.txcb.lib.base.AppManager;
import com.txcb.lib.base.utils.LogUtil;

import java.util.ArrayList;

/**
 * @author : Lambert
 * date   : 2020/10/25 8:28 PM
 */
public class TaskService extends Service {

    public static long activityTouchTime = 0;

    private Handler mHandler = null;

    @Override
    public void onCreate() {
        super.onCreate();

        LogUtil.d("TaskService: onCreate");
        activityTouchTime = System.currentTimeMillis();
        if (mHandler == null) {
            mHandler = new Handler();

            startTime();
        } else {
            mHandler.removeCallbacksAndMessages(null);
        }


    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    private void startTime() {
        LogUtil.d("startTime");
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {

                long time2 = System.currentTimeMillis() - activityTouchTime;
                ArrayList<BannerBean.BannerItem> bannerItemList = new ArrayList<>();
                bannerItemList.addAll(MainVM.Companion.getBannerItemList());

                LogUtil.d("time2 :" + time2);
                LogUtil.d("bannerItemList :" + bannerItemList.size());
                LogUtil.d("activityTouchTime :" + activityTouchTime);

                if (time2 >= 1000 * 30 && bannerItemList.size() > 0) {
                    activityTouchTime = System.currentTimeMillis();
                    Activity activity = AppManager.getInstance().currentActivity();
                    if (activity == null) {

                        LogUtil.d("startTime :return00000");
                        return;
                    }
                    if (!(activity instanceof PlayActivity)) {
                        LogUtil.d("AdvActivity false");
                        if (BaseActivity.Companion.isCanShowAdv()) {
//                            AdvActivity2.Companion.start(ActivityManager.getInstance().getLast(), bannerItemList);

                            PlayActivity.Companion.start(AppManager.getInstance().currentActivity(), bannerItemList);
                        }
//                        PlayActivity.start(ActivityManager.getInstance().getLast(),bannerItemList.get(2).localPath);

                    } else {
                        LogUtil.d("AdvActivity true");
                    }

                }
                startTime();
            }
        }, 2 * 1000);
    }


}
